/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   14-12-24 下午2:03 Created by dwzhang
 */
package com.jade.bss.beacon.impl;

import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.jade.bss.beacon.BeaconException;
import com.jade.bss.beacon.BeaconModel;
import com.jade.bss.beacon.BeaconModelDao;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.sql.SqlUtils;
import com.jade.framework.base.util.PagingList;

/**
 * Beacon型号数据访问接口，采用JPA方式。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 14-12-24 下午2:03
 */
@Named ("bss_beaconModelDao")
public class JpaModelDao
        implements BeaconModelDao
{
    @PersistenceContext (unitName = "default")
    protected EntityManager entityManager;

    public void addModel(BeaconModel model)
            throws BeaconException
    {
        entityManager.persist(model);
    }

    public void updateModel(BeaconModel model)
            throws BeaconException
    {
        entityManager.merge(model);
    }

    public BeaconModel getModel(String id)
    {
        return entityManager.find(BeaconModel.class, id);
    }

    public void removeModel(String id)
            throws BeaconException
    {
        BeaconModel model = getModel(id);
        if (model != null) {
            entityManager.remove(model);
        }
    }

    public List<BeaconModel> listModel(Criteria criteria)
    {
        String sql = "SELECT model FROM BeaconModel model";
        return SqlUtils.sqlQueryWithoutCount(sql, entityManager, criteria);
    }

    public PagingList<BeaconModel> queryModel(Criteria criteria)
    {
        String sql = "SELECT model FROM BeaconModel model";
        return SqlUtils.sqlQuery(sql, entityManager, criteria);
    }
}
